using System.Threading.Tasks;
using dotnet_graphql_demo.Model;
using GraphQL.Types;
using Mapster;

namespace dotnet_graphql_demo.Infrastructure.Graphql
{
    public class AccountMutation : ObjectGraphType<Account>
    {
        public async Task<Account> AddAccount(AccountInput accountInput, IAccountRepository accountRepository,
            IOwnerRepository ownerRepository)
        {
            Account account = accountInput.Adapt<Account>();
            account.Id = new System.Guid();
            Owner owner = new Owner();
            if (!accountInput.OwnerId.HasValue)
            {
                owner.Name = accountInput.Name;
                owner.Id = new System.Guid();
                owner = await ownerRepository.CreateOwnerAsync(owner);
            }
            account.OwnerId = owner.Id;
            await accountRepository.CreateAccountAsync(account);
            return await Task.FromResult(account);
        }
    }
}